package com.pureut.scheduling.controller;

import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;
import com.pureut.common.log.annotation.Log;
import com.pureut.common.log.enums.BusinessType;
import com.pureut.common.security.annotation.RequiresPermissions;
import com.pureut.scheduling.domain.BackWeight;
import com.pureut.scheduling.domain.SchedulingExecution;
import com.pureut.scheduling.domain.SchedulingExecutionOn;
import com.pureut.scheduling.domain.SchedulingTake;
import com.pureut.scheduling.domain.dto.SchedulingExecutionDto;
import com.pureut.scheduling.domain.dto.SchedulingProductionDto;
import com.pureut.scheduling.domain.vo.*;
import com.pureut.scheduling.service.ISchedulingProductionService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * @Description 生产排程
 * @Param
 * @Date 23-01-05
 * @Author hl
 **/
@RestController
@RequestMapping("/productionSchedule")
public class SchedulingProductionController extends BaseController {

    @Resource
    private ISchedulingProductionService schedulingProductionService;

    /**
     * 查询生产排程
     */
    @RequiresPermissions("schedule:plan:productionSchedule:list")
    @GetMapping("/list")
    public TableDataInfo list(SchedulingProductionVo schedulingProductionVo)
    {
        startPage();
        List<SchedulingProductionDto> list = schedulingProductionService.selectProductionList(schedulingProductionVo);
        return getDataTable(list);
    }

    /**
     * 根据编号获取详细信息(下达前)
     */
    @GetMapping(value = "/{productionId}")
    public AjaxResult getModelingInfo(@PathVariable long productionId)
    {
        return AjaxResult.success(schedulingProductionService.selectProductionById(productionId));
    }

    /**
     * 合并下达(下达前)
     */
   // @RequiresPermissions("schedule:plan:productionSchedule:combined")
    @GetMapping(value = "/combined/{productionIdS}")
    public AjaxResult getCombined(@PathVariable String productionIdS)
    {
        return AjaxResult.success(schedulingProductionService.selectProductionCombined(productionIdS));
    }

    /**
     * 手动下达排程单
     */
    @RequiresPermissions("schedule:plan:productionSchedule:manual")
    @Log(title = "生产排程", businessType = BusinessType.INSERT)
    @PostMapping("/manual")
    public AjaxResult manual(@RequestBody SchedulingExecutionOn schedulingExecutionVo){
        return toAjax(schedulingProductionService.insertSchedulingManual(schedulingExecutionVo));
    }

    /**
     * 合并下达排程单
     */
    @RequiresPermissions("schedule:plan:productionSchedule:consolidated")
    @Log(title = "生产排程", businessType = BusinessType.INSERT)
    @PostMapping("/combinedThe")
    public AjaxResult combinedThe(@RequestBody List<SchedulingExecutionOn> formList){
        return toAjax(schedulingProductionService.insertSchedulingCombinedThe(formList));
    }

    /**
     * 排程单撤回
     */
    @RequiresPermissions("schedule:plan:productionSchedule:recall")
    @Log(title = "生产排程", businessType = BusinessType.DELETE)
    @DeleteMapping("/{executionId}")
    public AjaxResult remove(@PathVariable("executionId") long executionId) throws Exception{
        return toAjax(schedulingProductionService.deleteExecutionById(executionId));
    }

    /**
     * 根据线别返回开工时间的时间戳
     */
    @GetMapping("/startWork")
    public AjaxResult getStartWork(long lineId,String materialCode,int widthCloth) throws ParseException, SQLException {
        return AjaxResult.success(schedulingProductionService.getStartWork(lineId,materialCode,widthCloth));
    }

    /**
     * 校验输入的分切数量是否符合规则
     */
    @GetMapping("/checkSlitting")
    public AjaxResult checkSlitting(int executionSlitting,int widthCloth) {
        return AjaxResult.success(schedulingProductionService.getCheckSlitting(executionSlitting,widthCloth));
    }

    /**
     * 根据排产重量and订单幅宽and分切数量返回预计工时and完工时间
     */
    @GetMapping("/finishWork")
    public AjaxResult getFinishWork(SchedulingExecutionMeterVo schedulingExecutionMeterVo) throws ParseException{
        return AjaxResult.success(schedulingProductionService.getFinishWork(schedulingExecutionMeterVo));
    }


    /**
     * 返回物料信息以及相应重量
     */
    @GetMapping("/materialRatio")
    public AjaxResult getMaterialRatio(String materialCode,double schedulingWeight){
        return AjaxResult.success(schedulingProductionService.getMaterialRatio(materialCode,schedulingWeight));
    }

    /**
     * 排程确认
     */
    @RequiresPermissions("schedule:plan:productionSchedule:confirm")
    @Log(title = "生产排程", businessType = BusinessType.OTHER)
    @PutMapping("/confirm")
    public AjaxResult confirm(@RequestBody SchedulingExecution SchedulingExecution) throws Exception{
        return toAjax(schedulingProductionService.confirmSchedulingExecution(SchedulingExecution.getExecutionId()));
    }

    /**
     * 查询生产排程(下达后)
     */
    @GetMapping("/listGive")
    public TableDataInfo listGive(SchedulingExecutionVo schedulingExecutionVo)
    {
        startPage();
        List<SchedulingExecutionDto> list = schedulingProductionService.selectSchedulingExecution(schedulingExecutionVo);
        return getDataTable(list);
    }

    /**
     * T+N锁定
     */
    @RequiresPermissions("schedule:plan:productionSchedule:T+N")
    @PostMapping("/lock")
    public AjaxResult lock(@RequestBody SchedulingLockVo schedulingLockVo){
        return toAjax(schedulingProductionService.insertSchedulingLock(schedulingLockVo));
    }

    /**
     * 甘特图展示
     */
    @GetMapping("/ganttChart")
    public AjaxResult ganttChart() throws ParseException
    {
        return AjaxResult.success(schedulingProductionService.selectGanttChart());
    }

    /**
     * 甘特图拖拽
     */
    @RequiresPermissions("schedule:plan:productionSchedule:ganttChartEdit")
    @PutMapping("/ganttChartEdit")
    public AjaxResult ganttChartEdit(@RequestBody GanttChartVo ganttChartVo)
    {
        return AjaxResult.success(schedulingProductionService.ganttChartEdit(ganttChartVo));
    }

    /**
     * 回传时间
     */
    @PutMapping("/backAdjust")
    public AjaxResult backAdjust(@RequestBody BackAdjustVo backAdjustVo)
    {
        return AjaxResult.success(schedulingProductionService.updateBackAdjust(backAdjustVo));
    }

    /**
     * 回传重量
     */
    @PutMapping("/backWeight")
    public AjaxResult backWeight(@RequestBody BackWeight backWeight)
    {
        return AjaxResult.success(schedulingProductionService.updateBackWeight(backWeight));
    }

    /**
     * 返回完工时间
     */
    @GetMapping("/finishTime")
    public Date finishTime(SchedulingTake schedulingTake) throws ParseException
    {
        return schedulingProductionService.selectFinishTime(schedulingTake);
    }

    /**
     * 查询bom返回仓库数据及所需用量
     */
    @GetMapping("/queryBom")
    public AjaxResult queryBom(String materialCode, double schedulingWeight, Long bomId, Long lineId,double trimmingWeight) {
        return AjaxResult.success(schedulingProductionService.queryBom(materialCode, schedulingWeight, bomId, lineId,trimmingWeight));
    }





}
